<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Transaksi
 *
 * @author Mirza
 */
class hutang extends Admin_My_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->model('hutang/model_hutang');
        $this->load->helper('hutang');
    }

    public function data_hutang() {
        $this->load->library("datagrid");

        $data = array();

        $this->log_event = "Membuka menu hutang";

        $data['srcPeriodStart'] = $this->input->get_post('srcPeriodStart') != "" ? $this->input->get_post('srcPeriodStart') : date("Y-m-d");
        $data['srcPeriodEnd'] = $this->input->get_post('srcPeriodEnd') != "" ? $this->input->get_post('srcPeriodEnd') : date("Y-m-d");
        $data['srcPeriodMode'] = $this->input->get_post('srcPeriodMode') != "";

        $field_name = array("k.nama", "d.tanggal_jatuh_tempo", "d.catatan");
        $field_title = array("Nama Karyawan", "Jatuh Tempo", "Catatan");
        $field_type = array("text", "date", "text");

        $filter_panel = new FilterPanel(base_url() . "hutang/hutang", $field_name, $field_title, $field_type);
        if ($data['srcPeriodMode'] == 'include')
            $where = " WHERE date(d.tanggal_hutang) BETWEEN '" . $data['srcPeriodStart'] . "' AND '" . $data['srcPeriodEnd'] . "' ";
        else
            $where = " WHERE TRUE ";
        $filter = $filter_panel->getFilterString();
        $where .= $filter ? " AND $filter " : "";

        $offset = $this->input->get("per_page") ? $this->input->get("per_page") : 0;
        $length = 30;
        $result = $this->model_hutang->getDataHutang($where, $offset, $length);
        $rowCount = $result->num_rows();

        $this->datagrid->setActionColumnsWidth(50);
        $this->datagrid->addMenu(GreyBox::createLink(base_url() . "index.php/hutang/data_hutang_add/", "", "Tambah Data", "add", "onclick", 1024, 600));
        $this->datagrid->addMenu("<a href=\"" . base_url() . "index.php/hutang/data_hutang_export/?" . $this->input->server("QUERY_STRING") . "\" class=\"report\" title=\"Export Data Hutang\">&nbsp;</a>");

        $this->datagrid->setColumns("No.", "Nama Karyawan", "Catatan", "Total Hutang", "Sisa Hutang", "Cicilan", "Tanggal Hutang", "Tanggal Jatuh Tempo");
        $this->datagrid->setPaginationDataNew($this->model_hutang->get_scalar("SELECT COUNT(*) as jml FROM data_hutang d $where", "jml"), $length, "hutang/hutang", $rowCount, $offset);
        $this->datagrid->setButtonExportVisible(false);

        if ($rowCount > 0) {
            $nomor = $offset + 1;

            foreach ($result->result() as $row) {
                $rowID = $row->id_hutang;
                if (!$is_popup) {
                    $this->datagrid->addRowAction("edit('" . $rowID . "')", "edit", "Edit Hutang");
                    $this->datagrid->addRowAction("hapus('" . $rowID . "')", "delete", "Hapus Hutang");
                } else {
                    $this->datagrid->onRowClickAction("parent.parent._obj_fSendValue('" . $rowID . "')");
                }

                $this->datagrid->addRowData(
                        $nomor++, $row->nama, $row->catatan, number_format($row->nominal_hutang, 0), number_format($row->sisa_hutang, 0), number_format($row->nominal_hutang / $row->jumlah_cicilan, 0), $row->tanggal_hutang, $row->tanggal_jatuh_tempo
                );
            }
        } else {
            $this->datagrid->setEmptyDataState("Tidak ada data");
        }

        $data['date_picker'] = new jDatePicker();
        $data['filter_panel'] = $filter_panel;

        $data['breadcrumb'] = $this->breadCrumb(true);
        $this->loadMenu('hutang/hutang', $data);
    }

    public function data_hutang_add($kode = "") {
        $data = array();
        
        if ($this->input->get("hapus_pelunasan") != ""){
            $this->model_hutang->deletePelunasan($this->input->get("hapus_pelunasan"));
        } else if ($this->input->post("btnSubmitPelunasan")){
            $vars = array(
                "id_hutang" => $kode,
                "tanggal_pelunasan" => "",
                "catatan" => "tidak ada catatan",
                "nominal_dibayarkan" => "",
                "id_reference" => ""                
            );

            $data = array();
            retrieve_request($data, $vars);
            $this->model_hutang->insertPelunasan($data);
        } else if ($this->input->post("btnSubmit")) {     
            
            $vars = array(
                "catatan" => "",
                "jumlah_cicilan" => 1,
                "nominal_hutang" => 0,
                "tanggal_jatuh_tempo" => "",
                "tanggal_hutang" => "",
                "nik" => ""
            );

            $data = array();
            retrieve_request($data, $vars);

            if ($kode == "")
                $kode = $this->model_hutang->insertUpdateHutang($data);
            else {                
                $this->model_hutang->insertUpdateHutang($data, $kode);
            }
            echo "<script>" . CloseGB(true) . "</script>";
        }
        
        if ($kode != "") {
            $data["data_pelunasan"] = $this->model_hutang->getDataPelunasan($kode);
            retrieve_data_hutang($data, $kode);
            $data["kode"] = $kode;
        } else {
            $data["tanggal_hutang"] = date("Y-m-d");
        }

        $data['gbkaryawan'] = $this->greybox->createLink(base_url() . 'index.php/popup/karyawan', '<span></span>', 'Cari Karyawan', 'find', '', 750, 450);

        $this->loadMenuHeaderPopup("hutang/hutang_add", $data);
    }

    public function hutang_delete($kode) {
        $this->model_hutang->deleteHutang($kode);
        echo "<script>alert('Data berhasil dihapus');</script>";
        echo "<script>location.href = '" . site_url("transaksi/quotation?" . $_SERVER["QUERY_STRING"]) . "'</script>";
    }

    public function data_hutang_export() {
        $data['srcPeriodStart'] = $this->input->get_post('srcPeriodStart') != "" ? $this->input->get_post('srcPeriodStart') : date("Y-m-d");
        $data['srcPeriodEnd'] = $this->input->get_post('srcPeriodEnd') != "" ? $this->input->get_post('srcPeriodEnd') : date("Y-m-d");
        $data['srcPeriodMode'] = $this->input->get_post('srcPeriodMode') != "";

        $data['srcPeriodStart'] = $this->input->get_post('srcPeriodStart') != "" ? $this->input->get_post('srcPeriodStart') : date("Y-m-d");
        $data['srcPeriodEnd'] = $this->input->get_post('srcPeriodEnd') != "" ? $this->input->get_post('srcPeriodEnd') : date("Y-m-d");
        $data['srcPeriodMode'] = $this->input->get_post('srcPeriodMode') != "";

        $field_name = array("k.nama", "d.tanggal_jatuh_tempo", "d.catatan");
        $field_title = array("Nama Karyawan", "Jatuh Tempo", "Catatan");
        $field_type = array("text", "date", "text");

        $filter_panel = new FilterPanel(base_url() . "hutang/hutang", $field_name, $field_title, $field_type);
        if ($data['srcPeriodMode'] == 'include')
            $where = " WHERE date(d.tanggal_hutang) BETWEEN '" . $data['srcPeriodStart'] . "' AND '" . $data['srcPeriodEnd'] . "' ";
        else
            $where = " WHERE TRUE ";

        $filter = $filter_panel->getFilterString();
        $where .= $filter ? " AND $filter " : "";

        $queryExport = "SELECT d.nik, k.nama, d.catatan, d.nominal_hutang, d.jumlah_cicilan,
                d.nominal_hutang - (select COALESCE(sum(nominal_dibayarkan),0) from pelunasan_hutang where id_hutang = d.id_hutang) as sisa_hutang
                FROM data_hutang d
                left join mt_karyawan k on k.nik = d.nik
                $where order by d.is_lunas asc, d.tanggal_hutang";

        $this->export($queryExport);
    }

}

?>
